package com.codexsoft.test;

import com.codexsoft.test.entities.Country;
import com.codexsoft.test.jpamodel.EMF;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.html.basic.*;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.PropertyModel;
import javax.persistence.EntityManager;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: lotos
 * Date: 22.08.11
 * Time: 12:35
 * To change this template use File | Settings | File Templates.
 */
public class CountryTabPanel extends Panel {
    private List<Country> arrayList;
    private String name;

    public CountryTabPanel(String id) {
        super(id);
        InitializeCollection();

        ListView listView = new ListView<Country>("countryList", arrayList) {
            @Override
            protected void populateItem(ListItem<Country> countryListItem) {
                Country c = countryListItem.getModelObject();
                AjaxLink link = new AjaxLink("countryLinkList") {
                    @Override
                    public void onClick(AjaxRequestTarget ajaxRequestTarget) {

                    }
                };
                link.add(new Label("countryLinkLabel", new PropertyModel(c, "name")));
                countryListItem.add(link);
            }
        };
        add(listView);
        add(new CountryEditPanel("countryInfo"));
    }

    private void InitializeCollection() {
        EntityManager em = EMF.get().createEntityManager();
        try {
            String query = "select c from Country c";
            List<Country> countryList = em.createQuery(query).getResultList();
            arrayList = new ArrayList<Country>();
            arrayList.addAll(countryList);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            em.close();
        }

    }

}


